package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.BaseActivityRegisterRequest;
import com.ruoyi.community.domain.CpcActivity;
import com.ruoyi.community.domain.CpcActivityParticipate;
import com.ruoyi.community.mapper.CpcActivityMapper;
import com.ruoyi.community.mapper.CpcActivityMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.mapper.CpcActivityParticipateMapper;
import com.ruoyi.community.domain.CpcActivityParticipate;
import com.ruoyi.community.service.ICpcActivityParticipateService;

/**
 * 党建活动签到Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-22
 */
@Service
public class CpcActivityParticipateServiceImpl implements ICpcActivityParticipateService 
{
    @Autowired
    private CpcActivityParticipateMapper cpcActivityParticipateMapper;

    @Autowired
    private CpcActivityMapper cpcActivityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询党建活动签到
     * 
     * @param id 党建活动签到ID
     * @return 党建活动签到
     */
    @Override
    public CpcActivityParticipate selectCpcActivityParticipateById(String id)
    {
        return cpcActivityParticipateMapper.selectCpcActivityParticipateById(id);
    }

    /**
     * 查询党建活动签到列表
     * 
     * @param cpcActivityParticipate 党建活动签到
     * @return 党建活动签到
     */
    @Override
    public List<CpcActivityParticipate> selectCpcActivityParticipateList(CpcActivityParticipate cpcActivityParticipate)
    {
        return cpcActivityParticipateMapper.selectCpcActivityParticipateList(cpcActivityParticipate);
    }

    @Override
    public List<SysUser> selectCpcActivityParticipateListById(String id)
    {
        List<CpcActivityParticipate> listEnroll= cpcActivityParticipateMapper.selectCpcActivityParticipateListById(id);
        List<SysUser> listUser = new ArrayList<>();
        SysUser query = new SysUser();
        for (CpcActivityParticipate cpcActivityParticipate : listEnroll) {
            query.setUserId(cpcActivityParticipate.getUserId());
            query.setCommunityId(SecurityUtils.getCommunityId());
            List<SysUser> participateUsers= sysUserMapper.selectUserOnlyOnUser(query);
            listUser.addAll(participateUsers);
        }
        return listUser;
    }

    /**
     * 新增党建活动签到
     * 
     * @param baseActivityRegisterRequest 党建活动签到
     * @return 结果
     */
    @Override
    public int insertCpcActivityParticipate(BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int hasRegistered = cpcActivityParticipateMapper.hasRegistered(loginUser.getUserId().toString(), baseActivityRegisterRequest.getActivityId());
        if(hasRegistered == 1){
            return -1;
        }else{
            CpcActivity CpcActivity = cpcActivityMapper.selectCpcActivityById(baseActivityRegisterRequest.getActivityId());
            Long realRegisterCode = CpcActivity.getRegisterCode();
            if (!baseActivityRegisterRequest.getRegisterCode().equals(realRegisterCode)){
                return -999;
            }else {
                CpcActivityParticipate CpcActivityParticipate = new CpcActivityParticipate();
                CpcActivityParticipate.setUserId(loginUser.getUserId());
                CpcActivityParticipate.setActivityId(baseActivityRegisterRequest.getActivityId());
                CpcActivityParticipate.setTime(baseActivityRegisterRequest.getTime());

                Long participateNumber = CpcActivity.getParticipationNumber() + 1;
                CpcActivity.setParticipationNumber(participateNumber);
                cpcActivityMapper.updateCpcActivity(CpcActivity);

                return cpcActivityParticipateMapper.insertCpcActivityParticipate(CpcActivityParticipate);
            }
        }
    }

    /**
     * 查询是否已经签到
     *
     * @param baseActivityRegisterRequest 活动签到详情
     * @return 结果
     */
    @Override
    public boolean hasRegistered(BaseActivityRegisterRequest baseActivityRegisterRequest)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int hasRegistered = cpcActivityParticipateMapper.hasRegistered(loginUser.getUserId().toString(), baseActivityRegisterRequest.getActivityId());
        return hasRegistered==1;
    }

    /**
     * 修改党建活动签到
     * 
     * @param cpcActivityParticipate 党建活动签到
     * @return 结果
     */
    @Override
    public int updateCpcActivityParticipate(CpcActivityParticipate cpcActivityParticipate)
    {
        return cpcActivityParticipateMapper.updateCpcActivityParticipate(cpcActivityParticipate);
    }

    /**
     * 批量删除党建活动签到
     * 
     * @param ids 需要删除的党建活动签到ID
     * @return 结果
     */
    @Override
    public int deleteCpcActivityParticipateByIds(String[] ids)
    {
        return cpcActivityParticipateMapper.deleteCpcActivityParticipateByIds(ids);
    }

    /**
     * 删除党建活动签到信息
     * 
     * @param id 党建活动签到ID
     * @return 结果
     */
    @Override
    public int deleteCpcActivityParticipateById(String id)
    {
        return cpcActivityParticipateMapper.deleteCpcActivityParticipateById(id);
    }
}
